﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebNext.Domain.Entities;
using System.Text;
using WebNext.Domain;
using WebNext.UI.Utils.Validators;
using WebNext.UI.Properties;
using WebNext.UI.Utils;

namespace WebNext.UI.Models.Admin
{
	public class AdminSettingsViewModel : MainViewModel
	{
		public string UserName
		{
			get;
			private set;
		}

		public string Email
		{
			get;
			private set;
		}

		public string Password
		{
			get;
			private set;
		}

		public string RepeatedPassword
		{
			get;
			private set;
		}

		public bool IsUserNameValid
		{
			get;
			private set;
		}

		public bool IsEmailValid
		{
			get;
			private set;
		}

		public bool IsPasswordValid
		{
			get;
			private set;
		}

		public bool IsRepeatedPasswordValid
		{
			get;
			private set;
		}

		public AuthorInfoViewModel AuthorInfo
		{
			get;
			set;
		}

		private AdminSettingsViewModel() : base(true)
		{
			this.IsUserNameValid = true;
			this.IsEmailValid = true;
			this.IsPasswordValid = true;
			this.IsRepeatedPasswordValid = true;

			this.ViewType = MainViewType.Admin;
			
			this.PageTitle = string.Format(Resources.TitleFormat, RuntimeSettings.SitePrefix, Resources.Admin);
			this.PageName = Resources.Admin;
			this.PageDescription = Resources.AdminSettings;
		}

		public AdminSettingsViewModel(AdminSettings settings) : this()
		{
			this.UserName = settings.UserName;
			this.Email = settings.Email;
			this.Password = AdminSettings.PasswordStub;
			this.RepeatedPassword = AdminSettings.PasswordStub;
		}

		public AdminSettingsViewModel(string userName, string email, string password, string repeatedPassword) : this()
		{
			this.UserName = userName;
			this.Email = email;
			this.Password = password;
			this.RepeatedPassword = repeatedPassword;
		}

		public ValidationResult Validate()
		{
			ValidationResult result = new ValidationResult();

			IValidator userNameValidator = new UserNameValidator(this.UserName);
			ValidationResult userNameValidationResult = userNameValidator.Validate();
			if (!userNameValidationResult.IsValid)
			{
				this.IsUserNameValid = false;
				result.Merge(userNameValidationResult);
			}

			IValidator emailValidator = new EmailValidator(this.Email);
			ValidationResult emailValidationResult = emailValidator.Validate();
			if (!emailValidationResult.IsValid)
			{
				this.IsEmailValid = false;
				result.Merge(emailValidationResult);
			}

			if (this.Password != AdminSettings.PasswordStub || this.RepeatedPassword != AdminSettings.PasswordStub)
			{
				IValidator passwordValidator = new PasswordValidator(this.Password);
				ValidationResult passwordValidationResult = passwordValidator.Validate();
				if (!passwordValidationResult.IsValid)
				{
					this.IsPasswordValid = false;
					result.Merge(passwordValidationResult);
				}

				if (this.Password != this.RepeatedPassword)
				{
					this.IsRepeatedPasswordValid = false;
					result.AddError(Properties.Resources.RepeatedPasswordMustBeTheSameAsPassword);
				}
			}

			return result;
		}
	}
}